﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ManageYWZL : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["ID"] == null)
            {
                Response.Write("<script>alert('非法登录！');location='../Default.aspx'</script>");
            }
            else
            {
                using (DataClassesDataContext ds = new DataClassesDataContext())
                {
                    DropDownList1.DataSource = from q in ds.YeWuZiLiao
                                               where (q.parentID == null || q.parentID == -1)
                                               select q;
                    DropDownList1.DataTextField = "mingcheng";
                    DropDownList1.DataValueField = "id";
                    DropDownList1.DataBind();

                    if (DropDownList1.SelectedValue != null)
                    {
                        gvQueInfo.DataSource = from q in ds.YeWuZiLiao
                                               where q.parentID == Convert.ToInt32(DropDownList1.SelectedValue)
                                               orderby q.id descending
                                               select q;
                        gvQueInfo.DataBind();
                    }
                }
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            //DropDownList1.DataSource = from q in ds.YeWuZiLiao
            //                           where (q.parentID == null || q.parentID == -1)
            //                           select q;
            //DropDownList1.DataTextField = "mingcheng";
            //DropDownList1.DataValueField = "id";
            //DropDownList1.DataBind();

            if (DropDownList1.SelectedValue != null)
            {
                gvQueInfo.DataSource = from q in ds.YeWuZiLiao
                                       where q.parentID == Convert.ToInt32(DropDownList1.SelectedValue)
                                       where SqlMethods.Like(q.mingcheng, "%" + TextBox1.Text.Trim().ToString() + "%")
                                       orderby q.id descending
                                       select q;
                gvQueInfo.DataBind();
            }
        }
    }
    protected void gvQueInfo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Response.Redirect("ChangeYWZL.aspx");
    }
    protected void gvQueInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            var rd = from q in ds.YeWuZiLiao
                     where q.id == Convert.ToInt32(gvQueInfo.DataKeys[e.RowIndex].Value)
                     select q;
            ds.YeWuZiLiao.DeleteAllOnSubmit(rd);
            ds.SubmitChanges();
        }
        Response.Redirect("ManageYWZL.aspx");
    }
    protected void gvQueInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            gvQueInfo.PageIndex = e.NewPageIndex;
            if (DropDownList1.SelectedValue != null)
            {
                gvQueInfo.DataSource = from q in ds.YeWuZiLiao
                                       where q.parentID == Convert.ToInt32(DropDownList1.SelectedValue)
                                       where SqlMethods.Like(q.mingcheng, "%" + TextBox1.Text.Trim().ToString() + "%")
                                       orderby q.id descending
                                       select q;
                gvQueInfo.DataBind();
            }
        }

    }
}